package gamehistory.challenge;

import java.util.HashMap;

import edu.neu.ccs.demeterf.lib.List;

import scg.gen.AccountAcceptTrans;
import scg.gen.AccountRefundTrans;
import scg.gen.AccountSolveTrans;
import scg.gen.History;
import scg.gen.OfferTrans;
import scg.gen.PlayerKickedEvent;
import scg.gen.PlayerTrans;
import scg.gen.ProvideTrans;
import scg.gen.ReofferTrans;
import scg.gen.SolveTrans;
import gamehistory.HistoryAssimilation;
import gamehistory.player.PlayerModel;
import gamehistory.player.PlayerModelAssimilation;

/**
 * 
 * @author Brent Kersanske
 *
 */
public class ChallengeModelAssimilation extends HistoryAssimilation {

	private static final String CHALLENGES = "challenges";
	private static final String ACCEPTED_CHALLENGES_FILE_END = "_Accepted_Challenges";
	private static final String FLOATING_CHALLENGES_FILE_END = "_Floating_Challenges";
	
	private HashMap<Integer, ChallengeModel> challenges;
	private PlayerModelAssimilation players;
	
	/**
	 * Constructor.
	 * @param gameHistory
	 * @param players
	 */
	public ChallengeModelAssimilation(History gameHistory, PlayerModelAssimilation players) {
		super(CHALLENGES);
		this.players = players;
		assimilate(gameHistory);
	}
	
	protected void assimilate(History gameHistory) {
		this.challenges = new HashMap<Integer, ChallengeModel> ();
		super.assimilate(gameHistory);
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param playerTransactions
	 */
	protected void handlePlayerTransactions(int roundNumber, PlayerTrans playerTransactions) {
		PlayerModel PlayerModel = players.getPlayerModel(playerTransactions.getId().getId());
		if(!playerTransactions.getOfferTrans().isEmpty()) {
			handleOfferTransactions(PlayerModel, playerTransactions.getOfferTrans());
		}
		if(!playerTransactions.getReofferTrans().isEmpty()) {
			handleReofferTransactions(PlayerModel, playerTransactions.getReofferTrans());
		}
		if(!playerTransactions.getProvidedTrans().isEmpty()) {
			handleProvideTransactions(PlayerModel, playerTransactions.getProvidedTrans());
		}
		if(!playerTransactions.getSolvedTrans().isEmpty()) {
			handleSolveTransactions(PlayerModel, playerTransactions.getSolvedTrans());
		}
	}

	/**
	 * 
	 * @param roundNumber
	 * @param playerName
	 * @param offerTransactions
	 */
	protected void handleOfferTransactions(PlayerModel PlayerModel, List<OfferTrans> offerTransactions) {
		for(OfferTrans offerTransaction : offerTransactions) {
			ChallengeModel newChallenge = new ChallengeModel(PlayerModel.getPlayerName(), offerTransaction);
			this.challenges.put(offerTransaction.getChallengeid(), newChallenge);
		}
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param playerName
	 * @param reofferTransactions
	 */
	protected void handleReofferTransactions(PlayerModel PlayerModel, List<ReofferTrans> reofferTransactions) {
		for(ReofferTrans reofferTransaction : reofferTransactions) {
			this.challenges.get(reofferTransaction.getChallengeid()).assimilateReofferTransaction(PlayerModel.getPlayerName(), reofferTransaction);
		}
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param playerName
	 * @param provideTransactions
	 */
	protected void handleProvideTransactions(PlayerModel PlayerModel, List<ProvideTrans> provideTransactions) {
		for(ProvideTrans provideTransaction : provideTransactions) {
			this.challenges.get(provideTransaction.getChallengeid()).assimilateProvideTransaction(PlayerModel.getPlayerName(), provideTransaction);
		}
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param playerName
	 * @param solveTransactions
	 */
	protected void handleSolveTransactions(PlayerModel PlayerModel, List<SolveTrans> solveTransactions) {
		for(SolveTrans solveTransaction : solveTransactions) {
			this.challenges.get(solveTransaction.getChallengeid()).assimilateSolveTransaction(PlayerModel.getPlayerName(), solveTransaction);
		}
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param accountSolveTransaction
	 */
	protected void handleAccountSolveTransaction(int roundNumber, AccountSolveTrans accountSolveTransaction) {
		this.challenges.get(accountSolveTransaction.getChallengeId()).assimilateAccountSolveTransaction(accountSolveTransaction);
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param accountAcceptTransaction
	 */
	protected void handleAccountAcceptTransaction(int roundNumber, AccountAcceptTrans accountAcceptTransaction) {
		PlayerModel PlayerModel = players.getPlayerModel(accountAcceptTransaction.getAcceptor());
		this.challenges.get(accountAcceptTransaction.getChallengeId()).assimilateAccountAcceptTransaction(PlayerModel.getPlayerName(), accountAcceptTransaction);
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param accountRefundTransaction
	 */
	protected void handleAccountRefundTransaction(int roundNumber, AccountRefundTrans accountRefundTransaction) {
		// TODO: Do something when players are refunded.
	}
	
	/**
	 * 
	 * @param roundNumber
	 * @param kickedEvent
	 */
	protected void handlePlayerKicked(PlayerKickedEvent kickedEvent) {
	 // TODO: Do something when players are kicked.	
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void buildXMLModel() {
		for(ChallengeModel challenge : this.challenges.values())	{
			challenge.buildModel();
			if(challenge.isAccepted()) {
				this.assimilatedModel.addChildModel(challenge);			
			}
		}
	}

	@Override
	protected String getAssimilationType() {
		return CHALLENGES;
	}

	@Override
	public String retrieveFileOutName() {
		return ACCEPTED_CHALLENGES_FILE_END;
	}
}